﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Evolution.Structure;
using Evolution.Structure.Globe.Details.Cities;
using Evolution.Structure.Globe;
using Evolution.Structure.Details.Cities;
using Evolution.Structure.Globe.Fields;
using Evolution.Structure.Globe.Details;
using Evolution.Structure.Globe.Units;
using EvolutionBaseType;
using EvolutionBaseTypes.Interfaces;

namespace EvolutionLogic.Handler
{
    class CityHandle
    {
        public City CityData { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        public CityHandle(City city)
        {
            this.CityData = city;
        }


        /// <summary>
        /// add buildings to building queue
        /// </summary>
        /// <param name="unitToBuild"></param>
        public void AddUnitToBuildingQueue(Building buildingToConstruct)
        {
            CityData.ConstructingQueue.Enqueue(buildingToConstruct);
        }

        /// <summary>
        /// Add units to building queue
        /// </summary>
        /// <param name="unitToBuild"></param>
        public void AddUnitToBuildingQueue(BaseUnit unitToRecruite)
        {
            CityData.ConstructingQueue.Enqueue(unitToRecruite);
        }

        /// <summary>
        /// list buildable units
        /// </summary>
        /// <returns></returns>
        public List<BaseUnit> ListBuildableUnits(List<Technology> availableTechnology)
        {
            throw new NotImplementedException();
            //check available Technology and the buildings => first technology => then building (result is a subset of technology listing)
        }

        /// <summary>
        /// list buildable buildings
        /// </summary>
        /// <returns></returns>
        public List<Building> ListBuildableBuildings(List<Technology> availableTechnology)
        {
            throw new NotImplementedException();
            //Check Technology which is available and return the list
        }

        /// <summary>
        /// Calculate Buildings Bonus
        /// </summary>
        /// <returns></returns>
        private Resources CalculateBuildingsBonus()
        {

            Resources buildingBonus = new Resources();
            //foreach (var building in CityData.Buildings) //run through all buildings in a town to sum up the bonus
            //{
            //    buildingBonus = buildingBonus + building.Benefits;
            //}
            return buildingBonus;
        }

        /// <summary>
        ///  calculate harvested base Points by citizens
        /// </summary>
        /// <returns></returns>
        private Resources SumHarvestedBasePoints()
        {
            Resources harvestedBaseResources = new Resources();
            foreach (var item in CityData.Citizens)
            {
                if (item.Type.Equals(CitizenType.Worker) && item.ParentWorldItem != null)
                {
                    harvestedBaseResources = harvestedBaseResources + (item.ParentWorldItem as Field).HarvestableResources;
                }
                else if (item.Type.Equals(CitizenType.Researcher))
                {
                    harvestedBaseResources.ResearchPoints.Value = harvestedBaseResources.ResearchPoints.Value + 1;
                }
                else if (item.Type.Equals(CitizenType.Philosopher))
                {
                    harvestedBaseResources.CulturePoints.Value = harvestedBaseResources.CulturePoints.Value + 1;
                }
            }

            return harvestedBaseResources;
        }

        /// <summary>
        /// A new Citizen is set as a Researcher by default.
        /// </summary>
        internal void IncreasePopulation()
        {
            CityData.Citizens.Add(new Citizen() { Type = CitizenType.Researcher, Position = CityData.Position, ParentWorldItem = CityData });
        }

        ///// <summary>
        ///// Increase influenced Area
        ///// </summary>
        ///// <param name="position"></param>
        ///// <param name="field"></param>
        ///// <returns></returns>
        //public Dictionary<Point, Field> IncreaseInfluencedArea(Point position, Field field)
        //{
        //    if (!CityData.ByCultureInfluencedArea.ContainsKey(position))
        //    {
        //        CityData.ByCultureInfluencedArea.Add(position, field);
        //    }
        //    return CityData.ByCultureInfluencedArea;
        //}

        ///// <summary>
        ///// Decrease influenced Area
        ///// </summary>
        ///// <param name="position"></param>
        ///// <param name="field"></param>
        ///// <returns></returns>
        //public Dictionary<Point, Field> DecreaseInfluencedArea(Point position, Field field)
        //{
        //    if (!CityData.ByCultureInfluencedArea.ContainsKey(position))
        //    {
        //        CityData.ByCultureInfluencedArea.Remove(position);
        //    }
        //    return CityData.ByCultureInfluencedArea;
        //}

        

    }
}
